﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Notify;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Notify;

namespace ManagementApi.Controllers.ManagerPannel
{
    /// <summary>
    /// 系统通知相关业务接口
    /// </summary>
    [Route("/api/v1/manager/sysnotify")]
    [ApiController]
    [Authorize(Roles = "Manager")]
    public class SysNotifyController : ControllerBase
    {
        private readonly IMapper mapper;
        private SysNotifyService sysNotifyService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysNotifyService"></param>
        /// <param name="mapper"></param>
        public SysNotifyController(
             IMapper mapper,
             SysNotifyService sysNotifyService
            )
        {
            this.mapper = mapper;
            this.sysNotifyService = sysNotifyService;
        }

        /// <summary>
        /// 获取系统通知列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<SysNotifyDto>> GetSysNotifysByPage([FromQuery] NotifyQuery query, int pageNum = 1, int pageSize = 20)
        {
            var sysNotifies = sysNotifyService.GetSysNotifysByPage(query, pageNum, pageSize);
            var dtos = sysNotifies.AsPageDto(source => { return source.AsSysNotifyDto(); });
            return ApiResult<PageDto<SysNotifyDto>>.Success(dtos);
        }

        /// <summary>
        /// 获取系统通知详情
        /// </summary>
        /// <param name="id">查询条件</param>
        /// <returns></returns>
        [HttpGet("form/{id}")]
        public ApiResult<SysNotifyDto> GetSysNotifyById(string id)
        {
            var sysNotify = sysNotifyService.GetSysNotify(id);
            if (sysNotify == null)
            {
                return ApiResult<SysNotifyDto>.Failure("查询失败，通知不存在");
            }
            return ApiResult<SysNotifyDto>.Success(sysNotify.AsSysNotifyDto());
        }
    }
}